package com.txsj.scan.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.google.gson.Gson;
import com.greendao.TagGroupInfoDao;
import com.txsj.scan.Constants;
import com.txsj.scan.R;
import com.txsj.scan.adapter.UnbindLabelAdapter;
import com.txsj.scan.base.BaseActivity;
import com.txsj.scan.bean.LabelBean;
import com.txsj.scan.bean.TagGroupInfo;
import com.txsj.scan.db.GreenDaoUtils;
import com.txsj.scan.event.Action;
import com.txsj.scan.presenter.UnbindLabelPresenter;
import com.txsj.scan.view.IUnbindLabelView;
import com.txsj.scan.widget.TitleBarView;
import com.txsj.scan.widget.dialog.LabelIntroduceDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * Created by wangchuangjun on 2018/12/12
 * Desc: 未绑定标签详情
 */

public class UnbindLabelActivity extends BaseActivity<UnbindLabelPresenter> implements
        IUnbindLabelView {

    private RecyclerView recyclerView;
    private UnbindLabelAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_unbind_label);
        initView();
        initData();
    }

    private void initData() {
        EventBus.getDefault().register(this);

        if (null == getIntent().getStringArrayListExtra("data") || getIntent()
                .getStringArrayListExtra("data").size() == 0)
            return;
        List<String> list = getIntent().getStringArrayListExtra("data");

        Bundle bundle = getIntent().getBundleExtra("bundle");
        List<LabelBean> labelBeans = (List<LabelBean>) bundle.getSerializable("label");

        adapter.setLabelBeanList(labelBeans);

        //缓存标签组信息
        presenter.cacheTagGroupInfo();
    }

    private void initView() {
        mTitle = (TitleBarView) findViewById(R.id.mTitle);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new UnbindLabelAdapter(this);
        recyclerView.setAdapter(adapter);
        mTitle.setRightOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new LabelIntroduceDialog(UnbindLabelActivity.this).show();
            }
        });
        adapter.setListener(new UnbindLabelAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                LabelBean labelBean = adapter.getLabelBeanList().get(position);
                filterLabelBean(labelBean);
            }
        });
    }

    /**
     * 判断labelBean的数据是否已经完成过预绑定
     *
     * @param labelBean
     */
    private void filterLabelBean(LabelBean labelBean) {

        TagGroupInfo info = null;
        List<TagGroupInfo> list = GreenDaoUtils.getSingleTon().getmDaoSession()
                .getTagGroupInfoDao().queryBuilder()
                .where(TagGroupInfoDao.Properties.Group_id.eq(labelBean.getGroupId()))
                .list();
        if (null != list && list.size() > 0) {
            info = list.get(0);
        }

        Intent intent = new Intent(UnbindLabelActivity.this, LabelActivity.class);
        intent.putExtra("data", labelBean);
        if (null != info && info.getId() != 0) {
            intent.putExtra("info", new Gson().toJson(info));
        }
        startActivity(intent);
    }

    @Override
    protected UnbindLabelPresenter providePresenter() {
        return new UnbindLabelPresenter(this);
    }

    @Override
    public Context context() {
        return this;
    }

    @Override
    public void onLoading() {
        showLoading();
    }

    @Override
    public void onSuccess() {
        hideLoading();
    }

    @Override
    public void onError(String msg) {
        hideLoading();
        showErrorMsg(msg);
    }

    //订阅方法，当接收到事件的时候，会调用该方法
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMain(Action event) {
        switch (event.getType()) {
            case Constants.up_remove_data:
                LabelBean labelBean = (LabelBean) event.getContent();
                adapter.removeData(labelBean);
                break;
//            case Constants.up_reAdd_data:
//                LabelBean labelBean1 = (LabelBean) event.getContent();
//                adapter.reAddData(labelBean1);
//                break;
        }
        LabelBean labelBean = (LabelBean) event.getContent();
        adapter.removeData(labelBean);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
